package com.iris.live.services.services.impl;

import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.data.repositories.CrontabRepository;
import com.iris.live.services.data.repositories.BIExtractRepository;
import com.iris.live.services.services.CrontabService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class CrontabServiceImpl implements CrontabService {
    private static final Logger logger = LoggerFactory.getLogger(CrontabServiceImpl.class);

    @Autowired
    private BIExtractRepository BIExtractRepository;

    @Resource(name="ActivityCrontabRepositoryImpl")
    private CrontabRepository activityCrontabRepositoryImpl;

    @Resource(name="FinancialFunnelCrontabRepositoryImpl")
    private CrontabRepository financialFunnelCrontabRepositoryImpl;

    @Resource(name="SaleFunnekCrontabRepositoryImpl")
    private CrontabRepository saleFunnekCrontabRepositoryImpl;

    public CrontabServiceImpl() {
    }

    @Override
    public String createDayData(String date) {
        return this.BIExtractRepository.extractDailyData(date);
    }

    @Override
    public String createMonthData(String date) {
        return this.BIExtractRepository.extractMonthlyData(date);
    }

    @Override
    public String createDayData(String date, String name) {
        return this.BIExtractRepository.extractDailyData(date, name);
    }

    @Override
    public String createMonthData(String date, String name) {
        return this.BIExtractRepository.extractMonthlyData(date, name);
    }

    @Override
    public String updateActivityKPIInfo(Map<String, Object> context) {
        try {
            activityCrontabRepositoryImpl.execute(context);

            return "success";
        } catch (Exception e) {
            logger.error("统计活动KPI异常。", e);
            return "false";
        }
    }

    @Override
    public String updateFinancialFunnelKPIInfo(String date) {
        try {
            String[] values = date.split("-");

            Map<String, Object> context = new HashMap<>();
            context.put("startDate", DateFormatUtils.getFirstDayOfMonth(values[0], values[1]));
            context.put("endDate", DateFormatUtils.getEndDayOfMonth(values[0], values[1]));
            financialFunnelCrontabRepositoryImpl.execute(context);

            return "success";
        } catch (Exception e) {
            logger.error("金融漏斗KPI异常。", e);
            return "false";
        }
    }

    @Override
    public String updateSaleFunnelKPIInfo(String date) {
        try {
            String[] values = date.split("-");

            Map<String, Object> context = new HashMap<>();
            context.put("startDate", DateFormatUtils.getFirstDayOfMonth(values[0], values[1]));
            context.put("endDate", DateFormatUtils.getEndDayOfMonth(values[0], values[1]));
            saleFunnekCrontabRepositoryImpl.execute(context);

            return "success";
        } catch (Exception e) {
            logger.error("销售漏斗KPI异常。", e);
            return "false";
        }
    }
}
